﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLySimDienThoai
{
    public partial class frmTimKiem : Form
    {
        SqlConnection m_sqlConnect;
        DataSet m_DataSet;
        DataSet m_DataSetTimKiem1;
        DataSet m_DataSetTimKiem2;
        SqlDataAdapter m_dataAdapter;
        DataTable m_dataTable;
        DataTable m_dataTableTimKiem1;
        DataTable m_dataTableTimKiem2;
        public frmTimKiem()
        {
            InitializeComponent();
            m_sqlConnect = new SqlConnection("Server=ITQUANGNAM\\SQLEXPRESS;Integrated security=SSPI;database=QuanLySimDT");
            m_DataSet = new DataSet();
            m_DataSetTimKiem1= new DataSet();
            m_DataSetTimKiem2 = new DataSet();
            try
            {
                m_sqlConnect.Open();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi mở kết nối:" + ex.ToString());
            }
            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult resul = MessageBox.Show("Thoát khỏi chương trình tìm kiếm?", "Tìm Kiếm Sim - Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (resul == DialogResult.Yes)
            {
                this.Dispose();
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (cbxNhaMang.Text != "" && cbxLoaiSim.Text != "")
            {
                m_dataAdapter = new SqlDataAdapter("select * from Sim where len(SoSim)='" + cbxLoaiSim.Text + "' and NhaMang = '" + cbxNhaMang.Text + "'", m_sqlConnect);
                m_DataSetTimKiem1.Reset();
                try
                {
                    m_dataAdapter.Fill(m_DataSetTimKiem1);
                    m_dataTableTimKiem1 = m_DataSetTimKiem1.Tables[0];
                    dgvwTimKiem.DataSource = m_dataTableTimKiem1.DefaultView;
                }
                catch (Exception ex) { MessageBox.Show(ex.ToString()); }
            }
            else
            {
                if (chbxSoCuoi.Checked == true)
                {
                    m_dataAdapter = new SqlDataAdapter("select * from Sim where SoSim like '%" + txtSoSim.Text + "'", m_sqlConnect);
                    m_DataSetTimKiem2.Reset();
                    try
                    {
                        m_dataAdapter.Fill(m_DataSetTimKiem2);
                        m_dataTableTimKiem2 = m_DataSetTimKiem2.Tables[0];
                        dgvwTimKiem.DataSource = m_dataTableTimKiem2.DefaultView;
                    }
                    catch (Exception ex) { MessageBox.Show(ex.ToString()); }
                }
            }   
        }

        private void frmTimKiem_Load(object sender, EventArgs e)
        {
            string strSelect = "select distinct TenNhaMang from NhaMang";
            m_dataAdapter = new SqlDataAdapter(strSelect, m_sqlConnect);
            m_DataSet.Clear();
            try
            {
                m_dataAdapter.Fill(m_DataSet);
                m_dataTable = m_DataSet.Tables[0];
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
            if (m_dataTable != null)
            {
                foreach (DataRow row in m_dataTable.Rows)
                {
                    cbxNhaMang.Items.Add(row[0].ToString());
                }
            }
        }

        private void dgvwTimKiem_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}